package com.djakapm.swing.component.table;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;

import com.djakapm.helper.StringComparator;

public class Table extends JTable {
	private static final long serialVersionUID = 8180609887558376114L;
	private ColumnSortListener csl = new ColumnSortListener();
	private Vector<String> columns;
	
	class ColumnSortListener extends MouseAdapter{
		boolean isSortAsc = false;
		int sortCol;
		int columnsCount;
		
		@Override
		 public void mouseClicked(MouseEvent e) {
			  TableColumnModel colModel = Table.this.getColumnModel();
		      int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
		      int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
		      if (modelIndex < 0) {return;}
			  columnsCount = colModel.getColumnCount();

		      if (sortCol == modelIndex) {
		        isSortAsc = !isSortAsc;
		      }
		      else {
		        sortCol = modelIndex;
		      }
		      
		      Table.this.sort(modelIndex,isSortAsc);
		    }			 
	}
	
	
	public Table() {
		super();
		this.setup();
	}
	
	public Table(Vector<Vector<Object>> rows,Vector<String> columns) {
		super(new TableModel(new DefaultTableModel(rows,columns),new StringComparator()));
		this.columns = columns;
		this.setup();
	}
	
	public Table(Vector<String> columns) {
		super(new TableModel(new DefaultTableModel(new Vector<Vector<Object>>(),columns),new StringComparator()));
		this.columns = columns;
		this.setup();
	}
	
	private void sort(int columnIndex,boolean asc) {
		((TableModel)this.getModel()).setSortColumn(columnIndex,asc);
	}
	
	private void setup() {
		this.setAutoResizeMode(AUTO_RESIZE_OFF);
		JTableHeader header = this.getTableHeader();
	    header.setUpdateTableInRealTime(true);
	    header.addMouseListener(this.csl);
	    header.setReorderingAllowed(true);
	}
	
	@Override
	public DefaultTableModel getModel() {
		return (DefaultTableModel) super.getModel();
	}
	
	public Table addRow(Object... cellValues) {
		Object[] row = null;
		int colCount = this.getColumnCount();
		
		row = new Object[this.getColumnCount()];
		
		if(colCount > cellValues.length) {
			for(int index=0;index<cellValues.length;index++) {
				row[index]=cellValues[index];
			}
			for(int index=cellValues.length;index<colCount;index++) {
				row[index] = "";
			}
		}
		else {
			for(int index=0;index<colCount;index++) {
				row[index]=cellValues[index];
			}
		}
		
		this.getModel().addRow(row);
		return this;
	}
	
	public Table addRow(Vector<Object> row) {
		this.getModel().addRow(row);
		return this;
	}
	
	/**
	 * Get the value of selected cell.
	 * @return Object
	 */
	public Object getValue() {
		return this.getValueAt(this.getSelectedRow(), this.getSelectedColumn());
	}
	
	/**
	 * Get the value of selected row.
	 * @return Vector<Object> row
	 */
	public Vector<Object> getRow() {
		int rowIdx = this.getSelectedRow();
		Vector<Object> row = null;
		if(rowIdx >= 0) {
			row = new Vector<Object>();
			for(int index=0;index<this.getColumnCount();index++) {
				row.addElement(this.getValueAt(rowIdx,index));
			}
		}
		return row;
	}
	
	/**
	 * Get all selected rows.
	 * @return Vector<Vector<Object>> rows
	 */
	public Vector<Vector<Object>> getRows(){
		int[] rowIdxs = this.getSelectedRows();
		Vector<Object> row = null;
		Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
		for(int rowIdx:rowIdxs) {
			if(rowIdx >= 0) {
				row = new Vector<Object>();
				for(int index=0;index<this.getColumnCount();index++) {
					row.addElement(this.getValueAt(rowIdx,index));
				}
				rows.addElement(row);
			}
		}
		return rows;
	}
	
	public void clear(){
		this.setModel(new TableModel(new DefaultTableModel(new Vector<Vector<Object>>(),this.columns),new StringComparator()));
	}
}
